package com.wn.service;

import com.wn.common.pojo.*;
import com.wn.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @BelongsProject: hurricane_travel
 * @BelongsPackage: com.wn.service
 * @Author: lhq
 * @Description: 私人定制的业务逻辑
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class CustomizeService {

    @Autowired
    private TravelCityDetailMapper travelCityDetailMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private ResterauntMapper resterauntMapper;
    @Autowired
    private ScenicSpotsMapper scenicSpotsMapper;


    //根据web层传递的目的地城市的名称查询各类信息
    public TravelCityDetail getListByDetails(String t) {
        //首先根据城市名拿到城市的id
        City city = cityMapper.selectByRegionName(t);
        //根据城市名查询数据库中该目的地详情信息
        return travelCityDetailMapper.getByCityId(city.getId());

    }
    //进行添加目的地详情操作
    @Transactional
    public Integer addNewCityDetail(String cityRegionName) {

        //获取该城市名的code
        City city = cityMapper.selectByRegionName(cityRegionName);
        System.out.println("获得的city是:"+city);

        //如果该城市已有详情就返回失败
        TravelCityDetail travelCityDetail = travelCityDetailMapper.getByCityId(city.getId());
        if(!StringUtils.isEmpty(travelCityDetail)){
            return 0;
        }
        //如果不存在该地区的详情表就执行全部添加    根据city的id查询该地区所有的酒店
        List<Hotel> hotelList = hotelMapper.getByCityRegionCode(city.getRegioncode());
        List<Resteraunt> resterauntList = resterauntMapper.getByCityRegionCode(city.getRegioncode());//根据city的id查询该地区所有的饭店
        List<ScenicSpots> scenicSpotList = scenicSpotsMapper.getByCityRegionCode(city.getRegioncode());//根据city的id查询该地区所有的景区
        //添加中间表数据
        TravelCityDetail tcd = new TravelCityDetail();
        tcd.setCityId(city.getId());
        travelCityDetailMapper.insert(tcd);
        //遍历集合 添加中间表
        for (Hotel h:hotelList) {
             //执行添加中间表操作
            hotelMapper.insertTempHotel(tcd.getId(),h.getId());
        }
        for (Resteraunt r:resterauntList) {
            //执行添加中间表操作
            resterauntMapper.insertTempResteraunt(tcd.getId(),r.getId());
        }
        for (ScenicSpots s:scenicSpotList) {
            //执行添加中间表操作
            scenicSpotsMapper.insertTempScenicSpots(tcd.getId(),s.getId());
        }

        return 1;
    }
}
